# -*- coding: utf-8 -*-
'''
@author: dyq
'''
from Common.htmlHlep import Find
from Common.tool import tool
from Cms.plus import  page,user
import  hashlib,datetime
from Models import  models


class SchoolMember:
    '''Represents any school member.'''
    def __init__(self, name, age):
        print '(Initialized SchoolMember: %s)' % self.name

class Teacher(SchoolMember):
    '''Represents a teacher.'''
    def __init__(self, name, age, salary):
        SchoolMember.__init__(self, name, age)
        self.salary = salary
        print '(Initialized Teacher: %s)' % self.name

    def tell(self):
        SchoolMember.tell(self)
        print 'Salary: "%d"' % self.salary


@page.page_render(user.Admin())
def Index(request):
        '''
                     首页
        '''
        return Find.View(request)

def Login(request):
        '''
                     登录页
        '''
        if len(models.Sys_Info.objects.filter())==0:
            return Find.Redirect("/AdminMain/Info/Initialization")
        
        return Find.View(request)

@page.page_render(user.Admin())
def EditNickName(request):
        '''
                     用户个人管理
        '''
        userinfo=user.user().GetUser(request)
        account=models.User_Account.objects.get(UserId=userinfo.UserId)
        nickName=request.POST.get("NickName")
        if userinfo.NickName!=nickName:
            count=len(models.User_Account.objects.filter(NickName=nickName))
            if count==0:
                account.NickName=nickName
                account.save()
                user.user().Login(account, request)
                return Find.JsonX(False,"操作成功")
            else:
                return Find.JsonX(True,"您没有操作权限")   
        else:
            return Find.JsonX(True,"账号没有修改")
        
@page.page_render(user.Admin())
def UnApproveLogin(request):
        '''
                     用户个人管理
        '''
        userinfo=user.user().GetUser(request)
        rowid=request.GET.get("Rowid")
        login=models.User_Login.objects.get(Rowid=rowid)
        if login.UserId.UserId==userinfo.UserId:
            login.State=0
            login.save()
            return Find.JsonX(False,"操作成功")
        else:
            return Find.JsonX(True,"您没有操作权限")    
  
@page.page_render(user.Admin())
def ApproveLogin(request):
        '''
                     用户个人管理
        '''
        userinfo=user.user().GetUser(request)
        rowid=request.GET.get("Rowid")
        login=models.User_Login.objects.get(Rowid=rowid)
        if login.UserId.UserId==userinfo.UserId:
            login.State=1
            login.save()
            return Find.JsonX(False,"操作成功")
        else:
            return Find.JsonX(True,"您没有操作权限")        

@page.page_render(user.Admin())
def Site(request):
        '''
        用户个人管理
        '''
    
        userinfo=user.user().GetUser(request)
        model={"userinfo":userinfo,
               "List":models.User_Login.objects.filter(UserId=userinfo)}
        
        return Find.View(request,model)

def LogOut(request):
        '''
                     退出登录
        '''
        user.user().LogOut(request)
        return Find.Redirect("/AdminMain/Home/Login")

def DoLogin(request):
        pwd =tool.Md5(request.POST.get("loginPwd"))
        userList= models.User_Login.objects.filter(Account=request.POST.get("loginId") , PassWord=pwd,State=1)
        if len(userList)==0:
            return Find.Json({"IsOk":False,"txt":"账号和密码不匹配"})
        else:
            if userList[0].UserId.State==1:
                user.user().Login(userList[0].UserId, request)
                
                return Find.Json({"IsOk":True})
            else:
                return Find.Json({"IsOk":False,"txt":"账号未开通或者被停用"})
    
def ChangePassword(request):
        '''
                    修改密码
        '''
        info=models.User_Login.objects.get(Rowid=request.GET.get("Rowid"))
        return Find.View(request,info)
    
def DoChangePassword(request):
        '''
                     修改密码
        '''
        oldPwd =tool.Md5(request.POST.get("OldPwd"))
        info= models.User_Login.objects.get(Rowid=request.POST.get("RowId"))
        if info.PassWord==oldPwd:
            info.PassWord=tool.Md5(request.POST.get("NewPwd"))
            info.save()
            return Find.JsonX(False,"旧密码不正确")
        else:
            return Find.JsonX(True,"旧密码不正确")
        
def AddLogin(request):
        '''
                    添加登录账号
        '''
    
        return Find.View(request)
    
def DoAddLogin(request):
        '''
                     修改密码
        '''
        userinfo=user.user().GetUser(request)
        account=request.POST.get("Account")
        passWord=request.POST.get("NewPwd")
        count=len(models.User_Login.objects.filter(Account=account))
        if count==0:
            info = models.User_Login(Rowid=tool.guid(),UserId=userinfo,
                                 Account=account,State=1,Types=0,
                                 CreateTtime=datetime.datetime.now())
            info.PassWord=hashlib.md5(passWord).hexdigest().upper()
            info.save()
            return Find.JsonX(False,"添加成功")
        else:
            return Find.JsonX(True,"已经存在登录账号")

